/*
 *检查用户信息
 * @Author: mikey.zhaopeng
 * @Date:   2016-02-21 18:24:00
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-04-15 12:35:19
 */

define([
    '../../app/framework/bean/common',
    '../../app/framework/bean/user',
    '../../app/framework/bean/entrustaccounts',
    '../../app/framework/bean/organization'
], function(
    commonModel,
    userModel,
    entrustaccountsModel,
    organizationModel
) {
    function me() {
        var self = this;
        /**
         * { 用户是否登录 }
         *
         * @method     valicateIsLogin
         * @param      {Function}  callback  { description }
         */
        self.valicateIsLogin = function(callback) {
            commonModel.timestamp(function(result) {
                if (result.flag == 'fail') {
                    var sl = window.localStorage.getItem('language');
                    var rememberMeObj = window.localStorage.getItem('rememberMe');
                    var reportTitle = window.localStorage.getItem('report-list-22');
                    localStorage.clear();
                    window.localStorage.setItem('language', sl);
                    window.localStorage.setItem('rememberMe', rememberMeObj);
                    window.localStorage.setItem('report-list-22', reportTitle);
                    sessionStorage.clear();
                    window.location.href = 'login.html?gotoUrl=' + window.location.href;
                } else {
                    callback();
                }
            });
        }

        /**
         * { 检查是否绑定BM }
         *
         * @method     valicateBusiness
         * @param      {Function}  callback  { description }
         */
        self.valicateBusiness = function(callback) {
            // 添加如果有没有绑定账号,则跳转到绑定账号的页面
            userModel.queryUser(function() {
                if (userModel.isUidesigner() || userModel.isOuteruidesigner()) { //3 美宣,不进行验证
                    callback();
                } else {
                    entrustaccountsModel.businessManagers(function(result) {
                        var gotoAccount = false;
                        if (!result.businessmanagers || result.businessmanagers.length == 0) {
                            gotoAccount = true;
                            if(window.location.hash=='#!unbind' ||window.location.hash=='#!chgpwd'||window.location.hash=='#!profile' ){
                                gotoAccount=false;
                            }
                        }
                        if (gotoAccount) {
                            window.location.hash.indexOf('account') != -1 ? '' : window.location.hash = '#!account';
                            callback('account')
                            return;
                        } else {
                            callback();
                        }
                    },function(result){
                        if(result.errorCode=='60121'||result.errorCode=='60101'){
                            window.location.hash.indexOf('account') != -1 ? '' : window.location.hash = '#!account';
                            callback('account')
                        }
                    });
                }
            });
        }

        self.checkUser = function(callback){
            userModel.queryUser(function(result){
                console.log("查询用户回调");
                if (result.roleId && result.roleId.indexOf(1) != -1){
                    console.log("管理员角色");
                    //如果是管理员 先要校验公司信息是否填写，若未填写则跳转到企业信息页面
                    organizationModel.searchOrganization(function(result){
                        console.log("查询企业回调");
                        if (result && result.status == 0){
                            //未设置管理员信息 就跳转到设置页面
                            window.location.hash.indexOf('orgInfo') != -1 ? '' : window.location.hash = '#!orgInfo';
                            callback('orgInfo');
                            return;
                        } else {
                            console.log("不需要回填企业信息");
                            //否则校验BM
                            self.valicateBusiness(callback);
                        }
                    });
                } else {
                    console.log("非管理员角色");
                    //否则校验BM
                    self.valicateBusiness(callback);
                }
            })
        }
    }
    return new me();
});
